package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.resource.cci.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

public class AddState extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddState() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//response.setContentType("text/html");
		request.setCharacterEncoding("gb2312");
		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		String user_id=request.getParameter("Id");
		String datetime=request.getParameter("time");
		String content=request.getParameter("content");
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://127.0.0.1:3306/angryfish";
			String user="root";
			String pass="";
			java.sql.Connection conn=DriverManager.getConnection(url,user,pass);
			java.sql.PreparedStatement ps1=((java.sql.Connection) conn).prepareStatement("insert into state values(?,?,?,?)");
			ps1.setString(1, "0");
			ps1.setString(2,user_id);
			ps1.setString(3,content);
			ps1.setString(4, datetime);
			ps1.executeUpdate();
			out.println("Add successfully!");
		    
			//out.print("fjafjfj");
			java.sql.PreparedStatement ps2=((java.sql.Connection) conn).prepareStatement("select StateNumber from state where Id=? and time=?");
			ps2.setString(1,user_id);
			ps2.setString(2,datetime);
			java.sql.ResultSet rs = ps2.executeQuery();
			while(rs.next()){
			rs.getString(1);
			}
		}catch(SQLException e){
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");*/
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
